package net.mihome.app.ui.view;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.adapter.ListViewRssAdapter;
import net.mihome.app.bean.NewsList;
import net.mihome.app.bean.RssResource;
import net.mihome.app.bean.RssType;
import net.mihome.app.common.UIHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MihomeRssTypeView extends ListView {
	private Handler handler;
	private ListViewRssAdapter adapter;
	private List<RssResource> listData = new ArrayList<RssResource>();
	private AppContext appContext;
	private int begin=0;
	private int num=10;
	private int newsTypeId=NewsList.CATALOG_ALL;
	private MyViewLinster myViewLinster;
	public MihomeRssTypeView(Context context, AttributeSet attrs) {
		super(context, attrs);;
		afterInit();
		// TODO Auto-generated constructor stub
	}
	public MihomeRssTypeView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		afterInit();
		// TODO Auto-generated constructor stub
	}
	private void afterInit(){
		adapter =new  ListViewRssAdapter(getContext(),listData);
		setAdapter(adapter);
		setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                                    long arg3) {
                RssType blog = null;
                // 判断是否是TextView
                if (view instanceof TextView) {
                    blog = (RssType) view.getTag();
                } else {
                    TextView tv = (TextView) view
                            .findViewById(R.id.blog_listitem_title);
                    blog = (RssType) tv.getTag();
                }
                if (blog == null)
                    return;

                // 跳转到博客详情
                UIHelper.showUrlRedirect(view.getContext(), blog.getUrl());
            }
        });
		appContext=(AppContext) getContext().getApplicationContext();
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message msg) {
				// TODO Auto-generated method stub
				 	List<RssResource> newList=(List<RssResource>) msg.obj;
				 	listData.clear();
				 	listData.addAll(newList);
					adapter.notifyDataSetChanged();
					if(myViewLinster!=null){
						myViewLinster.onLoadComplete();
					}
				return false;
			}
		});
	}
	public void initData(){
		if(myViewLinster!=null){
			myViewLinster.onBeforeLoad();
		}
        new AsyncTask<String, Integer, List<RssResource>>() {
            @Override
            protected List<RssResource> doInBackground(String... strings) {
                return getCheckedRssResources();
            }

            @Override
            protected void onPostExecute(List<RssResource> rssResources) {
                super.onPostExecute(rssResources);
                listData.clear();
                listData.addAll(rssResources);
                adapter.notifyDataSetChanged();
                if(myViewLinster!=null){
                    myViewLinster.onLoadComplete();
                }
            }
        }.execute();
	}

	/**
	 * 找出用户选择的rss资源
	 * */
	public List<RssResource> getCheckedRssResources(){
		String[] rssResourceList=getResources().getStringArray(R.array.rss_resource_list);
		//所有rss资源类型
		List<RssResource> resLis=RssResource.parseList(rssResourceList); 
//		String rssCheckedString=appContext.getProperty("rss_checked");
		String rssCheckedString=appContext.getUserPropertie("rss_checked");
		//用户选择的资源类型
		List<String> checkList=new ArrayList<String>();
		//去除掉用户没有选择的资源类型
		Iterator<RssResource> iterator=resLis.iterator();
		if(rssCheckedString==null){
			rssCheckedString="";
		}
		while(iterator.hasNext()){
			RssResource rssResource=iterator.next();
			if(!rssCheckedString.contains(rssResource.getTitle())){
				iterator.remove();
			}
		}
		return resLis;
	}
	public MyViewLinster getMyViewLinster() {
		return myViewLinster;
	}
	public void setMyViewLinster(MyViewLinster myViewLinster) {
		this.myViewLinster = myViewLinster;
	}
	
}
